define(['Add'],function(Add){
	function shopCar(){
		Storage();
		chenked();
		duoxuan();
//		Add.shoppingADD();
		foot();
		xuanZhong();
	}
	//点击选中商品
	function chenked(){
		$('.checked input').on('change',function(){
			if($(this).attr('checked') == 'checked'){
				$(this).prev().addClass('xuanzhong');
				PriceJia.call(this);
			}else{
				$(this).prev().removeClass('xuanzhong');
				PricrJian.call(this);
			}
			
		})
		
	}
	//选中之后总价变化
	function PriceJia(){
		var $price = parseFloat($(this).parent().siblings('.cars_menu').find('.price span').text()),
			$sum_Price = parseFloat($('.sum_price span').text()),
			num = parseFloat($(this).parent().siblings('.cars_menu').find('.num').text().trim());
			$sum = $sum_Price + $price*num;
			console.log($sum)
		$('.sum_price span').text($sum.toFixed(2));
	}
	//取消选中总价变化
	function PricrJian(){
		var $price = parseFloat($(this).parent().siblings('.cars_menu').find('.price span').text()),
			$sum_Price = parseFloat($('.sum_price span').text()),
			num = parseFloat($(this).parent().siblings('.cars_menu').find('.num').text().trim());
			$sum = $sum_Price - $price*num;
//			console.log(num)
		$('.sum_price span').text($sum.toFixed(2));
	}
	//点击全选
	function duoxuan(){
		$('.sum_checkbox input').on('change',function(){
			moren.call(this);
		})
	}
	//默认全选中
	function xuanZhong(){
		$('#mush').attr('checked','checked');
		moren.call($('#mush'));
	}
	//全选选中后的操作
	function moren(){
		if($(this).attr('checked') == 'checked'){
			$('.checked input').attr('checked','checked');
			$('.checked label').addClass('xuanzhong');
			$('.sum_checkbox label').addClass('xuanzhong');
			duoxuanPrice();
		}else{
			$('.checked input').removeAttr('checked');
			$('.checked label').removeClass('xuanzhong');
			$('.sum_checkbox label').removeClass('xuanzhong');
			$('.sum_price span').text((0).toFixed(2));
		}
	}
//	点击全选价格发生变化
	function duoxuanPrice(){
		var $price = 0;
		for(var i=0;i<$('.cars_menu .price span').length;i++){
			var price = parseFloat($('.cars_menu .price span').eq(i).text());
			var num = parseFloat($('.cars_menu .num').eq(i).text());
			$price += price*num;
//			console.log($price)
		}
		$('.sum_price span').text($price.toFixed(2));
	}
	/**********************localStorage************************/
	function Storage(){
		if(localStorage.getItem('shop_goods')){
			$('#none').css('display','none').siblings().css('display','block');
			var obj = JSON.parse(localStorage.getItem('shop_goods')).product;
			var html = '';
			for(var i in obj){
				html += '<li class="cars" classify="'+obj[i].classify+'"><div class="checked">'+
				'<label for="'+obj[i].Id+'"></label>'+
				'<input type="checkbox" name="1" id="'+obj[i].Id+'" /></div><div class="cars_img">'+
				'<img src="'+obj[i].img+'"/></div><div class="cars_menu"><p cla'+
				'ss="name">'+obj[i].name+'</p><p class="price">￥<span>'+obj[i].price+'</span></p>'+
				'<div class="jiajian"><div class="jian"></div><div class="num">'+obj[i].count+'</div>'+
				'<div class="jia"></div></div></div></li>';
			}
			$('#cars_list').html(html)
			addition();
			addition2();
		}
	}
	
	
	//点击数量增加
	function addition(){
		$('.jia').on('click',function(){
			addOrReduce.call(this,'.jia');
		})
	}
	function addOrReduce(or){
		//数量变化
			var num,classify,
			classify = $(this).parent().parent().parent().attr('classify'),
			id = $(this).parent().parent().parent().find('input').attr('id'),
			price = parseFloat($(this).parent().prev().find('span').text().trim()),
			sumPrice = parseFloat($('.sum_price span').text().trim()),
			data = JSON.parse(localStorage.getItem('shop_goods')),
			sum = parseFloat(localStorage.getItem('shoppingCarNum'));
			console.log(or)
			if(or == '.jia'){
				for(var i in data.product){
					if(classify ==data.product[i].classify && id == data.product[i].Id){
						counts = data.product[i].count;
						counts++;
						data.product[i].count = counts;
						localStorage.setItem('shop_goods',JSON.stringify(data));
					}
				}
				num = parseInt($(this).prev().text().trim());
				num++;
				sum++;
				localStorage.setItem('shoppingCarNum',sum);
				//总价变化
				if($(this).parent().parent().siblings('.checked').find('input').attr('checked') == 'checked'){
					sumPrice += price;
				}else{
					sumPrice += price*num;
				}
				$(this).prev().text(num);
			}else{
				console.log(data)
				for(var i in data.product){
					if(classify ==data.product[i].classify && id == data.product[i].Id){
						counts = data.product[i].count;
						counts--;
						if(counts == 0){
							var cla = data.product[i].classify + data.product[i].Id;
							localStorage.removeItem(cla);
							data.product.splice(i,1);
							localStorage.setItem('shop_goods',JSON.stringify(data));
							
						}else{
							data.product[i].count = counts;
							localStorage.setItem('shop_goods',JSON.stringify(data));
						}
						
					}
				}
				num = parseInt($(this).next().text().trim());
				
				num--;
				sum--;
				console.log(num,sum)
				localStorage.setItem('shoppingCarNum',sum);
				$(this).next().text(num);
				if($(this).parent().parent().siblings('.checked').find('input').attr('checked') == 'checked'){
					sumPrice -= price;
					if(sumPrice <= 0){
						sumPrice = 0.00;
						$('#mush').attr('checked',false);
						$('#mush').prev().removeClass('xuanzhong');
					}
					
				}else{
					sumPrice -= price*num;
				}
				
				if(num == 0){
					$(this).parent().parent().parent().remove();
					
				}
			}
			
			$('.sum_price span').text(sumPrice.toFixed(2));
		
			foot();
			//同时选中多选框
			$(this).parent().parent().siblings('.checked').find('input').attr('checked','checked');
			$(this).parent().parent().siblings('.checked').find('label').addClass('xuanzhong');
	}
	
	//点击数量减少
	function addition2(){
		$('.jian').on('click',function(){
			addOrReduce.call(this,'.jian')
		})
	}
	//点击加减时，footer数量也变化
	function foot(){
		var num = localStorage.getItem('shoppingCarNum');
		if(num == 0){
			$('#shop>span').css('display','none');
			$('#none').css('display','block').siblings().css('display','none');
		}else{
			$('#shop>span').css('display','inline-block');
			$('#none').css('display','none').siblings().css('display','block');
		}
		$('#shop>span').text();
	}
	
	/****shoppingCarNum数量为0是，页面隐藏***/
	return {
		shopCar : shopCar
	}
})